package com.quanyue.haircut.shop.job;

import com.quanyue.haircut.domain.entity.UserCoupon;
import com.quanyue.haircut.shop.mapper.CouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class HaircutJob {
    @Autowired
    private CouponUserMapper couponUserMapper;

    // 优惠券过期校检
    @Scheduled(cron = "0 0/30 * * * ?")
    public void checkCouponExpires() {
        List <UserCoupon> expireCoupon = new ArrayList <>();
        couponUserMapper.findAllValid().forEach(userCoupon -> {
            if (userCoupon.getExpireTime().compareTo(LocalDateTime.now()) <= 0)
                userCoupon.setDelFlag(true);
            expireCoupon.add(userCoupon);
        });
        couponUserMapper.updateAll(expireCoupon);
    }
}
